package com.rivulet.foreign.tools;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;

/**
 * {一句话形容此class功能}
 *
 * @author xujiac
 * @create 2021/8/19
 * @since 1.0.0
 */
public class FileTool {
    public final static boolean MultipartFileToFileSave(String fileDirPath,String user_id,MultipartFile multipartFile){
        boolean result=true;
        try {
            String filePath=fileDirPath+user_id+File.separator+multipartFile.getOriginalFilename();
            File file=new File(filePath);
            if(!file.getParentFile().exists()){
                file.getParentFile().mkdir();
            }
            file.createNewFile();
            multipartFile.transferTo(file);
        }catch (Exception e){
            result=false;
            e.printStackTrace();
        }
        return result;
    }
    public final static boolean FileDelete(String fileDirPath,String user_id){
        String path=fileDirPath+user_id;
        File file=new File(path);
        if(file.exists()){
            File[] files=file.listFiles();
            for(File one:files){
                if(one.isFile()&&one.exists()){
                    one.delete();
                }
            }
        }
        return true;
    }
    public final static boolean pictureSave(String fileDirPath,String user_id,MultipartFile multipartFile){
        try {
            String filePath=fileDirPath+"a_user_picture"+File.separator+user_id+".png";
            File file=new File(filePath);
            if(!file.getParentFile().exists()){
                file.getParentFile().mkdir();
            }
            file.createNewFile();
            multipartFile.transferTo(file);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }
    public final static BufferedImage getInputStream(String filepath){
        try {
            BufferedImage image = ImageIO.read(new FileInputStream(filepath));
            return image;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}